@push('title')
    <title>政策列表</title>
@endpush
@push('style2')
<style>


    .layui-table-cell{
        height: auto;
        overflow:visible;
        text-overflow:inherit;
        white-space:normal;
        word-break: break-all;
    }
</style>
@endpush
@extends('layout.layer')
@section('content')

    <form class="layui-form layui-form-pane1" action="" lay-filter="first" style="margin-bottom: 15px">

            <div class="layui-input-inline">
                <label class="layui-form-label">政策类型</label>
                <div class="layui-input-inline">
                    <select name="type" lay-filter="interest">
                        <option value="">所有</option>
                        <option value="1">特殊政策</option>
                        <option value="2">运价直连</option>
                    </select>
                </div>
            </div>

            <div class="layui-input-inline">
                <label class="layui-form-label">数据源</label>
                <div class="layui-input-inline">
                    <select name="status" lay-filter="interest">
                        <option value="0">所有</option>
                        <option value="1">蜗牛数据</option>
                    </select>
                </div>
            </div>

        <div class="layui-input-inline">
            <label class="layui-form-label">政策代码</label>
            <div class="layui-input-inline">
                <input type="text" name="policy_code" maxlength="" placeholder="" autocomplete="off" class="layui-input" >
            </div>
        </div>

        <div class="layui-input-inline">
            <label class="layui-form-label">航司</label>
            <div class="layui-input-inline">
                <input type="text" name="airline#like" maxlength="" placeholder="" autocomplete="off" class="layui-input" >
            </div>
        </div>

            <div class="layui-input-inline">
                <button class="layui-btn" id="search-btn" lay-submit lay-filter="*">搜索</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>

    </form>

    <div  id="policy-upload-form" style="display: none" >
        <form class="layui-form layui-form-pane1" action="" lay-filter="policy-upload-form" style="margin:10px;">

            <input type="hidden" name="path" value="" id="file-path">
            <div class="layui-input-inline">
                <label class="layui-form-label" style="width: 100px">是否保留原政策</label>
                <div class="layui-input-inline">
                </div>
            </div>
            <div class="layui-input-inline">
                <div class="layui-input-inline">
                    <input type="radio" name="delPolicy" value="0" title="保留原政策" checked>
                    <input type="radio" name="delPolicy" value="1" title="删除原政策" >
                </div>
            </div>

            <div class="layui-input-inline">
                <label class="layui-form-label"  style="width: 100px">请选择航程类型</label>
                <div class="layui-input-inline">
                </div>
            </div>
            <br>
            <div class="layui-input-inline">
                <div class="layui-input-inline">
                    <input type="radio" name="policyType" value="ROUND" title="往返" checked>
                </div>
            </div>
            <br>
            &nbsp;
            <br>
            <div class="layui-form-item">
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn layui-btn-normal" id="policy-upload-file"><i class="layui-icon"></i>选择文件</button>
                </div>
            </div>

        </form>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-inline">
            <button type="button" class="layui-btn" id="policy-upload-btn">政策导入</button>
        </div>
    </div>
    <table id="test2" lay-filter="test2"></table>
    <script type="text/html" id="result-option">

        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="policy-update">编辑</a>
<!--        @{{# if(d.status == 1){ }}
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="policy-status">无效</a>
        @{{# } }}
        @{{# if(d.status == 2){ }}
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="policy-status">有效</a>
        @{{# } }}-->

        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="policy-del">删除</a>
    </script>
@endsection
@push('script')
    <script>

        layui.use(['table','form','laydate','upload','layer'], function() {
            var $ = layui.$
                ,table = layui.table
                ,form = layui.form
                ,laydate = layui.laydate
                ,upload = layui.upload
                ,layer = layui.layer;


            //日期
            laydate.render({
                elem: '#day'
                ,type: 'date'
            });


            var tableData = {};

            table.render({
                id: 'test2'
                ,elem: '#test2'
                ,url: '/qnpolicy/ajaxLists'
                ,page: { //详细参数可参考 laypage 组件文档
                    curr: 1
                    ,groups: 20
                    ,first: 1
                    ,last: true
                    ,layout: ['limit', 'prev', 'page', 'next', 'count','refresh', 'skip'] //自定义分页布局
                }
                //,height: 300
                ,cellMinWidth: 80
                ,limit : 30
                //,skin: 'line'
                ,toolbar: false
                ,parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                    tableData =  res.data

                    return {
                        "code": res.errcode, //解析接口状态
                        "msg": res.errmsg, //解析提示文本
                        "count": res.meta.total, //解析数据长度
                        "data":res.data,
                    };


                }
                ,request: {
                    pageName: 'page' //页码的参数名称，默认：page
                    ,limitName: 'page_size' //每页数据量的参数名，默认：limit
                }
                ,cols: [[
                    {type: 'checkbox'}
                    // ,{field: 'id', 'title':'ID',width:50}
                    ,{field: 'platform', 'title':'所上平台',templet:function (row) {

                        var qnImg =  '<img src="/static/images/platform/qunar.bmp" style="width: 80px;height: auto;">';

                        switch (row.platform) {
                            case '10':
                            case '11': return '<img src="/static/images/platform/xiecheng.bmp" style="width: 80px;height: auto;">';
                            case '30':return qnImg+'<br>单程库存';
                            case '31': return qnImg+'<br>单程统一';
                            case '32': return qnImg+'<br>往返统一';
                        }
                        }}
                    ,{field: 'policy_id', 'title':'政策ID/政策代码',templet: function (row) {
                            return (row.policy_id != null ? row.policy_id +"<br>": '') +row.policy_code
                        }}
                    ,{field: 'depart_city_code', 'title':'出发',width: 70}
                    ,{field: 'arrived_city_code', 'title':'到达',width: 60}
                    ,{field: 'flight_code', 'title':'航班',width:90}
                    ,{field: 'share_flight_code', 'title':'共享航班',width: 90}
                    ,{field: 'depart_at', 'title':'日期',width: 110}
                    ,{field: 'cabin', 'title':'舱位',width: 60}
                    ,{field: 'seat', 'title':'座位数',width: 80}
                    ,{field: 'price', 'title':'票面价'}
                    ,{field: 'upload_msg', 'title':'上传结果'}
                    ,{field: 'status', 'title':'状态',width: 60,templet:function (row) {
                            switch (row.status) {
                                case '-1': return '删除';
                                case '0': return '挂起';
                                case '1': return '有效';
                                case '2': return '无效';
                                case '3': return '待确认';
                            }
                        }}
                    ,{field:'option', title:'操作',toolbar: '#result-option'}
                ]]

            });

            /**
             * 设置倒计时
             */
            setInterval(function () {

                var nowTime = new Date();
                for(var i = 0; i < tableData.length; i++) {

                    var orderTime = new Date(tableData[i].created_at);

                    var expireMinute = parseInt(((15 * 60 * 1000 - (nowTime - orderTime)) / 60000));

                    var expired = expireMinute + ':' + parseInt(((15 * 60 * 1000 - (nowTime - orderTime)) % 60000) / 1000);
                    if(expireMinute < 2) {

                        $('#expired_'+tableData[i].id).html(expired).attr("style","color: orangered");
                    } else {

                        $('#expired_'+tableData[i].id).html(expired)
                    }

                }

            },1000)

            //监听提交
            form.on('submit(*)', function(data){

                table.reload('test2',{
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where:data.field
                });

                return false;
            });

            //监听工具条
            table.on('tool(test2)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    ,layEvent = obj.event; //获得 lay-event 对应的值


                if (layEvent === 'policy-del') {

                    $.ajax({
                        url: '/qnpolicy/ajaxDelete'
                        , method: 'delete'
                        , data: {id:data.id,platform:data.platform,PolicyID:data.policy_id,_token:"{{csrf_token()}}"}
                        , dataType: 'json'
                        , success: function (data) {

                            layer.msg(data.errmsg, {
                                offset: '15px'
                                , icon: data.errcode == 0 ? 1 : 15
                                , time: 3000
                            }, function () {

                            });

                            if(data.errcode == 0) {

                                table.reload("test2")
                            }

                        },
                    });
                } else if (layEvent === 'policy-update') {
                    switch (data.platform) {
                        case "30" : {
                            window.parent.handleMenu({id:generateRandomString(6),path:"/qnpolicy/update?id="+data.id,name:'去哪政策编辑'});
                            break
                        }
                        case "31" : {
                            window.parent.handleMenu({id:generateRandomString(6),path:"/qnpolicy/singleUpdate?id="+data.id,name:'去哪普政编辑'});
                            break
                        }
                    }

                }  else if (layEvent === 'policy-status') {

                    $.ajax({
                        url: '/policyupload/itemUpdate'
                        , method: 'put'
                        , data: {id:data.id,status:data.status == 1 ? 2 : 1, platform: data.platform,Class:'Y', PolicyID: data.policy_id, _token:"{{csrf_token()}}" }
                        , dataType: 'json'
                        , success: function (res) {

                            layer.msg(res.errmsg, {
                                offset: '15px'
                                , icon: res.errcode == 0 ? 1 : 15
                                , time: 2000
                            }, function () {

                            });

                            if(res.errcode == 0) {

                                table.reload("test2")
                            }

                        },
                    });
                }

            });

            function generateRandomString(length) {
                const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
                let result = '';
                for (let i = 0; i < length; i++) {
                    const randomIndex = Math.floor(Math.random() * characters.length);
                    result += characters[randomIndex];
                }
                return result;
            }



            $('#policy-upload-btn').on('click',function () {

                layer.open({
                    type : 2,
                    content: '/qnpolicy/policyuploadform',
                    title : '政策导入',
                    area : [ "350px","350px" ],
                    shadeClose : true, // 点击遮罩关闭
                    //content : $('#policy-upload-form').html(),
                });
            });

        });
    </script>

@endpush